﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using ClientLib.MonitorState2;
using WpfDataBingding;

using Bus = WpfDataBingding.TaskServiceReference;

namespace ClientLib.Common
{
    class InitMonDevice
    {
          /*********************************************
         监控信息初始化
        *********************************************/
        public InitMonDevice(string username)
        {
            using (Bus.DataBusServiceClient client = new Bus.DataBusServiceClient())
            {
                Bus.UserMonTask usertask = client.QueryUserTask(username);
                //Bus.DeviceState[] statelist = client.QueryStates("u002");

                /*********************************************
                 需要控制连接失败信息！
                *********************************************/
                GeneratCollection(username);   
             
                GeneratDevice(usertask);
                GenerateSate(usertask,client);

                ThreadScan scanthread = (ThreadScan)Application.Current.Properties["ScanDevice"];
                Collection devcollection = (Collection)Application.Current.Properties["MonitorCollection"];

                int devcount = devcollection.GetDeviceCount();

                if (devcount > 0)
                {
                    scanthread.ScanTime = devcollection.GetScanTime();
                    scanthread.threadStart();
                }
            }  
        }


        /********************************************
         初始化监控代理集合
        *********************************************/
        public void GeneratCollection(string name)
        {
            Application.Current.Properties["MonitorCollection"] = new Collection(name);
            return;
        }

        /*********************************************
        初始化监控代理
        *********************************************/
        //public void GeneratMonitor(Bus.UserMonTask usertask)
        //{
        //    var monitor_list = usertask.MonTaskList.Select(c => new { mon_id=c.AgentName })
        //                                           .Distinct();


        //    Collection monCollection =(Collection)Application.Current.Properties["MonitorCollection"];

        //    MonitorDynamic monitor;
        //    foreach(var item in monitor_list)
        //    {
        //        monitor = new MonitorDynamic(item.mon_id);
        //        monCollection.AddNewMonitor(monitor);
        //    }
        //    return;
        //}

        /*********************************************
         初始化监控设备
         *********************************************/
        public void GeneratDevice(Bus.UserMonTask usertask)
        {
            var device_list = usertask.MonTaskList.Select(c => new { devid = c.DevID, monid = c.AgentName,devtype=c.DevType })
                                                  .Distinct()
                                                  .GroupBy(c => c.monid);
            {

            Collection monCollection = (Collection)Application.Current.Properties["MonitorCollection"];
            DeviceDynamic device;

            foreach(var item in device_list)
            {
                  foreach(var devitem in item)
                  {
                    device = new DeviceDynamic(devitem.devid,devitem.monid,devitem.devtype);
                    monCollection.AddDevice(device);
                  }
            }

            }

           return;
                
        }

        /*********************************************
        初始化监控状态 
        *********************************************/
        public void GenerateSate(Bus.UserMonTask usertask, Bus.DataBusServiceClient client)
        {  
           
            var state_list = usertask.MonTaskList.GroupBy(c => new { c.DevID, c.AgentName });
           {
               Collection monCollection = (Collection)Application.Current.Properties["MonitorCollection"];
               DeviceDynamic device;

               foreach (var item in state_list)
               {
                   
                   device = monCollection.GetDevice(item.Key.DevID,item.Key.AgentName);
                   DynamicState state;
                   Bus.AttributeValue attVal;

                   foreach(var deviceitem in item)
                   {

                       
                       {
                           Bus.DeviceState busquerystate = new Bus.DeviceState();

                           busquerystate.AgentID = deviceitem.AgentName;
                           busquerystate.DevType = deviceitem.DevType;
                           busquerystate.DevID = deviceitem.DevID;
                           busquerystate.Name = deviceitem.StateName;
                           

                           attVal=client.QueryStateTest(busquerystate);
                       }
                       if (attVal != null)
                       {
                           AttributeValue stateVal = new AttributeValue(attVal.Type, attVal.Value);

                           state = new DynamicState(deviceitem.StateName, deviceitem.DevID, deviceitem.AgentName, deviceitem.DevType, deviceitem.ViewType, deviceitem.DomainName, deviceitem.ChangeThreshold, deviceitem.TimeInterval);

                           int timeinterval = deviceitem.TimeInterval;

                           if (timeinterval < device.Scan_Time)
                           {
                               device.Scan_Time = timeinterval;
                           }
                           state.StateValue = stateVal;
                           device.AddMonitorState(state);
                       }
                   }/////查询单个状态值

               }////根据设备归类状态查询
           }
        }
    }
}
